import axios from "axios";
import router from "@/router";
import { error } from "@/message/messageApi";

const request = axios.create({
    baseURL: "https://apifoxmock.com/m1/5455088-5130247-default",
    // baseURL: "/api",
    timeout: 5000,
    headers: { Terminal: "h5" }
} as any);

request.interceptors.request.use((req: any) => {
    // console.log(req.url);
    return req;
});

request.interceptors.response.use(
    (res: any) => {
        if (res.data.apifoxError && res.data.apifoxError.code === "404") {
            router.replace('/');
            error("服务器错误");
            return Promise.reject(new Error("404 Not Found"));
        }
        return res.data;
    },
    (err: any) => {
        if (err.response && err.response.status === 404) {
            router.replace('/');
            error("服务器,或接口错误");
            return Promise.reject(err);
        }
        return Promise.reject(err);
    }
);

export default request;
